import { base, toast, promptBox} from 'nc-lightapp-front';

import ExportHtml from '../../../../../hrpub/common/utils/exportHtml';

import PrintTable from '../../../../../hrpub/common/utils/print'

import {hrAjax as ajax} from 'src/hrpub/common/utils/utils';

export default class HeaderAction {
    constructor(props) {
        this.props = props;
        this.state = {
            refpk:null
        }
    }
    willMount = () => {
       
    }
    didMount = () => {
      
    }

    didUpdate = () => {
    }

    /** 
     * 头部按钮点击事件
     * @param code 按钮编码
     */
    buttonVisible = (props,code) => {
        switch (code) {
            case 'setup': //设置
                //设置表格编辑状态
                props.editTable.setStatus(this.props.props.areacode,'edit')
                props.editTable.setColEditableByKey(this.props.props.areacode, ['isedit','ismustnotnull','isdefault'], true)

                // 判断初始化选中条数，等于0时渲染默认数据
                this.props.props.action.setUpTable()

                this.buttonTool(props,false)
                break;
            case 'save'://保存
                this.props.props.action.save()
                break;
            case 'refresh': //刷新
                this.props.props.action.refreshData()
                break;
            case 'cancel': //取消
                this.cancelFunction(props,true)
                break;
            case 'all_sel'://全选
                this.editSelectData(props)
                break;
            case 'all_unsel'://全消
                this.props.props.action.editAllUnsel(['isshow','isedit','isdefault','ismustnotnull'],false)
                break;
            case 'print': //打印
                this.printDate()
                break;
            case 'out'://输出
                this.outDate(props)
                break;
            case 'view'://预览
                this.viewData()
                break;
        }
     }
 
    /**
     * 取消提示框
     * @param flag 按钮状态
     */
    cancelFunction = (props,flag) => {
        promptBox({
            color: 'warning', 
            title: this.props.props.json['gx6008-000023'], //您确定要取消吗
            noCancelBtn: false, 
            beSureBtnName: this.props.props.json['gx6008-000024'], //确定
            cancelBtnName: this.props.props.json['gx6008-000025'], //取消
            hasCloseBtn:false, 
            beSureBtnClick: this.functionSure.bind(this,props,flag)
        })
    }

    /**
     * 确定取消触发函数
     */
    functionSure = (props,flag) => {
        // props.editTable.cancelEdit(this.props.props.areacode)
        this.props.props.action.cancelData()
        this.buttonTool(props,flag)
        props.editTable.setStatus(this.props.props.areacode,'browse')
    }

    /**
     * 按钮工具
     * @param flag 传入按钮显隐锁
     */
    buttonTool = (props,flag) => {
        this.props.props.action.updateButtonFlag(flag,() => {

            let buttonVisible = props.button.setButtonsVisible;
            
            let buttonAction = { //浏览态按钮装状态
                setup:flag,
                refresh: flag,
                print: flag
                
            }

            let buttonEdit = {//编辑态按钮状态
                save:!flag,
                cancel:!flag,
                all_sel:!flag,
                all_unsel:!flag
            }
            
            buttonVisible({
                autogen:'head_action',
                ...buttonAction,
                ...buttonEdit 
            })
        })
    }

    /**
     * 全选，循环基础数据改变选中状态
     */
    editSelectData = (props) => {
        let areacode = this.props.props.areacode
        this.props.props.initDate.rows.forEach((item,index) => {
            props.editTable.setValByKeyAndIndex(areacode, index, 'isshow',{value:true})
            props.editTable.setValByKeyAndIndex(areacode, index, 'isedit',{value:true})
            props.editTable.setValByKeyAndIndex(areacode, index, 'isdefault',{value:true})
            props.editTable.setEditableRowKeyByIndex(areacode, index, ['isedit','ismustnotnull','isdefault'], true)
            this.props.props.action.disableDefaultData()
        });
    }


    /**
     * 下拉框触发事件
     * @param value 下拉框value值
     */
    handleChange = (value) => {
        this.props.props.action.updatePlace(value,()=> {
            this.props.props.action.initTableDate()
        })
    }

    /**
     * 输出
     */
    outDate = (props) => {
        ExportHtml(document.querySelector('.u-table'), {
            // 转正项目-组织/转正项目-集团
            title: this.props.props.appConfig.type === 'ORGANI'?this.props.props.json['gx6008-000026']:this.props.props.json['gx6008-000027'],
            fileName: this.props.props.appConfig.type === 'ORGANI'?this.props.props.json['gx6008-000026']:this.props.props.json['gx6008-000027'],
            maker: this.props.props.json['gx6008-000028'],
            date: this.props.props.json['gx6008-000029']
        })
    }

    /**
     * 预览
     */
    viewData = () => {
        PrintTable(document.querySelector('.u-table'),
        {
            title:this.props.props.appConfig.type === 'ORGANI'?this.props.props.json['gx6008-000026']:this.props.props.json['gx6008-000027'],
            maker:this.props.props.json['gx6008-000028'],
            date:this.props.props.json['gx6008-000029'],
            maxColLen:8,
            beforeHtml:"",
            afterHtml:""
        })
    }

    /**
     * 打印
     */
    printDate = () => {
        PrintTable(document.querySelector('.u-table'),
        {
            title:this.props.props.appConfig.type === 'ORGANI'?this.props.props.json['gx6008-000026']:this.props.props.json['gx6008-000027'],
            maker:this.props.props.json['gx6008-000028'],
            date:this.props.props.json['gx6008-000029'],
            maxColLen:8,
            beforeHtml:"",
            afterHtml:""
        })
    }

    /**
     * 人力资源组织触发事件
     */
    handleChangePeople = (value) => {
        this.props.props.action.handleChangePeople(value)
    }
   
}